package com.dabai.udpshutdown2

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import com.dabai.udpshutdown2.databinding.ActivityMainBinding
import com.dabai.udpshutdown2.utils.UDPManager
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputLayout
import kotlin.concurrent.thread

class MainActivity : AppCompatActivity() {


    private lateinit var til1 : TextInputLayout
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        WindowCompat.setDecorFitsSystemWindows(window, false)
        super.onCreate(savedInstanceState)

        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setSupportActionBar(binding.toolbar)

        //初始化 UDP组件
        UDPManager.init(8847)
        UDPManager.setIPandPort("255.255.255.255",8846)


        val shutBtn1 = findViewById<Button>(R.id.shut1)
        val shutBtn2 = findViewById<Button>(R.id.shut2)
        val shutBtn3 = findViewById<Button>(R.id.shut3)
        til1 = findViewById(R.id.til1)


        shutBtn1.setOnClickListener {
            MaterialAlertDialogBuilder(this).setTitle("提示").setMessage("确认嘛？")
                .setNegativeButton("确认"){
                        _,_ ->

                    //网络操作 需要在线程内 操作，并且需要网络权限
                    thread {
                        sendMsg("shutdown:l")
                        runOnUiThread {

                        }
                    }
                }
                .setNeutralButton("取消",null)
                .show()

        }
        shutBtn2.setOnClickListener {
            MaterialAlertDialogBuilder(this).setTitle("提示").setMessage("确认嘛？")
                .setNegativeButton("确认"){
                        _,_ ->
                    //网络操作 需要在线程内 操作，并且需要网络权限
                    thread {
                        sendMsg("shutdown:r")
                        runOnUiThread {

                        }
                    }

                }
                .setNeutralButton("取消",null)
                .show()
        }
        shutBtn3.setOnClickListener {
            MaterialAlertDialogBuilder(this).setTitle("提示").setMessage("确认嘛？")
                .setNegativeButton("确认"){
                        _,_ ->

                    //网络操作 需要在线程内 操作，并且需要网络权限
                    thread {
                       sendMsg("shutdown:this")
                       runOnUiThread {

                       }
                   }

                }
                .setNeutralButton("取消",null)
                .show()
        }


    }


    fun sendMsg(message:String){
        if (til1.editText?.text?.isEmpty() == true){
            UDPManager.sendMsg(message)
        }else{
            UDPManager.sendMsg(message+"&user:"+ til1.editText!!.text)
        }

    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.menu_main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        return when (item.itemId) {
            R.id.action_settings -> {
                MaterialAlertDialogBuilder(this).setTitle("提示").setMessage("酷安@明灯花月夜").show()
                return true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }


}